<?php
/**
 * KayPaste
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category   KayPaste
 * @package    View
 * @subpackage Helpers
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */

/**
 * Imports
 * 
 * @see Zend_Auth
 * @see Zym_View_Helper_Html_Abstract
 */
require_once 'Zym/View/Helper/Html/Abstract.php';

/**
 * View helper for a userbox (You are logged in as ...)
 *
 * @category   KayPaste
 * @package    View
 * @subpackage Helpers
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */
class Kaypaste_View_Helper_Userbox extends Zym_View_Helper_Html_Abstract
{
    /**
     * Returns helper instance
     * 
     * @return Kaypaste_View_Helper_Userbox
     */
    public function userbox()
    {
        return $this;
    }
    
    /**
     * Renders userbox
     * 
     * @return string
     */
    public function toStringz($indent = null)
    {
        $indent = (null !== $indent)
                ? $this->_getWhitespace($indent)
                : $this->getIndent();
        
        $view = $this->getView();
        
        if ($id = Zend_Auth::getInstance()->getIdentity()) {
            $signoutUrl = $view->url(array(), 'logout', true);
            $dashboardUrl = $view->url(array(
                'module' => 'user',
                'controller' => 'dashboard',
                'action' => 'index'
            ), 'default', true);
            
            $html = $view->translate('Signed in as')
                  . ' <a href="' . $dashboardUrl . '">' . $id->username . '</a>'
                  . '<span class="separator"></span>'
                  . '<a href="' . $signoutUrl . '">'
                  . $view->translate('Sign out') . '</a>';
        } else {
            $signinUrl = $view->url(array(), 'login', true);
            $registerUrl = $view->url(array(
                'module' => 'user',
                'controller' => 'register',
                'action' => 'index'
            ), 'default', true);
            
            $html = $view->translate('Not signed in')
                  . '<span class="separator"> </span>'
                  . '<a href="' . $registerUrl . '">'
                  . $view->translate('Register') . '</a>'
                  . '<span class="separator"> </span>'
                  . '<a href="' . $signinUrl . '">'
                  . $view->translate('Sign in') . '</a>';
        }
        
        $html = "$indent<div id=\"userbox\">\n"
              . "$indent    $html\n"
              . "$indent</div>\n";
        
        return $html;
    }
    
    /**
     * Renders userbox
     * 
     * @return string
     */
    public function toString($indent = null)
    {
        $indent = (null !== $indent)
                ? $this->_getWhitespace($indent)
                : $this->getIndent();
        
        $view = $this->getView();
        
        $items = array();
        
        // check if user is logged in
        if ($id = Zend_Auth::getInstance()->getIdentity()) {
            $signoutUrl = $view->url(array(), 'logout', true);
            $dashboardUrl = $view->url(array(
                'module' => 'user',
                'controller' => 'dashboard',
                'action' => 'index'
            ), 'default', true);
            
            $items[] = $view->translate('Signed in as') . ' <a href="' . $dashboardUrl . '">' . $id->username . '</a>';
            $items[] = '<a href="' . $signoutUrl . '">' . $view->translate('Sign out') . '</a>';
        } else {
            $signinUrl = $view->url(array(), 'login', true);
            $registerUrl = $view->url(array(
                'module' => 'user',
                'controller' => 'register',
                'action' => 'index'
            ), 'default', true);
            
            $items[] = $view->translate('Not signed in');
            $items[] = '<a href="' . $registerUrl . '">' . $view->translate('Register') . '</a>';
            $items[] = '<a href="' . $signinUrl . '">' . $view->translate('Sign in') . '</a>';
        }
        
        $html = "$indent<ul class=\"userbox\">\n";
        foreach ($items as $item) {
            $html .= "$indent    <li>$item</li>\n";
        }
        $html .= "$indent</ul>\n";
        
        return $html;
    }
    
    /**
     * Magic method: Renders userbox
     * 
     * @return string
     */
    public function __toString()
    {
        return $this->toString();
    }
}